package com.ldy.job.utils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ImageUtils {
    /**
     * 去除白边
     *
     * @param address 图片路径
     * @param to      生成的图片路径
     * @throws Exception
     */
    public static void removeWhiteEdge(String address, String to){
        BufferedImage source = null;
        try {
            source = ImageIO.read(new File(address));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        int top = 0;
        int left = 0;
        int bottom = source.getHeight();
        int right = source.getWidth();

        //寻找最上面的标线,从左(0)到右，从上(0)到下
        for (int i = 0; i < source.getHeight(); i++)//行
        {
            boolean find = false;
            for (int j = 0; j < source.getWidth(); j++)//列
            {
                int pixel = source.getRGB(j, i);
                if (!IsWhite(pixel)) {
                    top = i;
                    find = true;
                    break;
                }

            }
            if (find) break;
        }
        //寻找最左边的标线，从上（top位）到下，从左到右
        for (int i = 0; i < source.getWidth(); i++)//列
        {
            boolean find = false;
            for (int j = top; j < source.getHeight(); j++)//行
            {
                int pixel = source.getRGB(i, j);
                if (!IsWhite(pixel)) {
                    left = i;
                    find = true;
                    break;
                }
            }
            if (find) break;
        }
        //寻找最下边标线，从下到上，从左到右
        for (int i = source.getHeight() - 1; i >= 0; i--)//行
        {
            boolean find = false;
            for (int j = left; j < source.getWidth(); j++)//列
            {
                int pixel = source.getRGB(j, i);
                if (!IsWhite(pixel)) {
                    bottom = i;
                    find = true;
                    break;
                }
            }
            if (find) break;
        }
        //寻找最右边的标线，从上到下，从右往左
        for (int i = source.getWidth() - 1; i >= 0; i--)//列
        {
            boolean find = false;
            for (int j = 0; j <= bottom; j++)//行
            {
                int pixel = source.getRGB(i, j);
                if (!IsWhite(pixel)) {
                    right = i;
                    find = true;
                    break;
                }
            }
            if (find) break;
        }
        BufferedImage sdf = source.getSubimage(left, top, right - left+1, bottom - top+1);
        try {
            ImageIO.write(sdf, "png", new File(to));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 判断是否白色（10点的容差）
     *
     * @param pixel
     * @return
     */
    public static boolean IsWhite(int pixel) {
        int[] rgb = new int[3];
        rgb[0] = (pixel & 0xff0000) >> 16; //r
        rgb[1] = (pixel & 0xff00) >> 8; //g
        rgb[2] = (pixel & 0xff); //b
        //RGB都为255为纯白
        if (rgb[0] > 245 && rgb[1] > 245 && rgb[2] > 245) return true;
        return false;
    }
}
